package demo3;

import java.util.LinkedList;
import java.util.Queue;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 石方旭
 * Date: 2022-08-04
 * Time: 19:34
 */
public class Test {

    static class Node{
        public int val;
        public Node left;
        public Node right;

        public Node(int val){
            this.val = val;
        }

    }
            //buildByLevelQueue

    //层序序列化
    public static Queue<String> levelSerial(Node head){
        Queue<String> ans = new LinkedList<>();
        if(head==null){
            ans.offer(null);
            return ans;
        }
        ans = level(head,ans);
        return ans;
    }
    public static Queue<String> level(Node head,Queue<String> ans){
        Queue<Node> queue = new LinkedList<>();
        queue.offer(head);
        ans.offer(String.valueOf(head.val));
        while(!queue.isEmpty()){
            Node cur = queue.poll();
            if(cur.left!=null){
                queue.offer(cur.left);
                ans.offer(String.valueOf(cur.left.val));
            }
            if(cur.right!=null){
                queue.offer(cur.right);
                ans.offer(String.valueOf(cur.right.val));
            }
        }
        return ans;
    }

    public static void buildByLevelQueue(Queue<String> ans){

    }


    public static void main(String[] args) {
        Node node1 = new Node(8);
        Node node2 = new Node(96);
        Node node3 = new Node(85);
        Node node4 = new Node(46);
        Node node5 = new Node(59);
        Node node6 = new Node(6);
        node1.left = node2;
        node1.right = node3;
        node2.left = node4;
        node2.right = node5;
        node4.left = node6;
        System.out.println(levelSerial(node1));
    }


}
